package com.gyg.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

/**
 * 线程池饥饿解决
 */
@Slf4j
public class ThreadPoolStarvation {

    static List<String> menu = Arrays.asList("地三鲜", "糖醋鱼", "宫保鸡丁", "辣椒炒肉");
    static Random random = new Random();

    public static String cooking() {
        return menu.get(random.nextInt(menu.size()));
    }

    public static void main(String[] args) {
        ExecutorService waiterPool = Executors.newFixedThreadPool(2);
        ExecutorService chefPool = Executors.newFixedThreadPool(1);
        for (int i = 0; i < 2; i++) {
            waiterPool.submit(() -> {
                String str = cooking();
                log.info("点菜->" + str);
                //区分同一线程池处理和不同线程池处理
                Future<?> future = chefPool.submit(() -> {
                    log.info("做菜->" + str);
                    return str;
                });
                try {
                    log.info("上菜->" + future.get());
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}
